package com.study.d012Components.tableview;

import java.util.HashMap;
import java.util.Map;

import com.sun.org.apache.bcel.internal.generic.NEW;

import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

/** tableview 使用MapValueFactory类加载map类型的数据
*@description
*@author xtayhame
*date 2020年3月14日
*version
*since v1.0
*/
public class TableViewWithMapDataMain2 extends Application{
	public static void main(String[] args) {
		launch(args);

	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane an = new AnchorPane();

		Button b1 =new Button("测试");

		TableView<Map<String, Object>> tableView = new TableView<Map<String, Object>>();


		an.getChildren().addAll(b1, tableView);
		AnchorPane.setTopAnchor(b1, 10.0);
		AnchorPane.setLeftAnchor(b1, 20.0);
		AnchorPane.setTopAnchor(tableView, 40.0);
		AnchorPane.setLeftAnchor(tableView, 20.0);


		stage.setScene(new Scene(an, 400, 500));
		stage.show();


		ObservableList<Map<String, Object>> datas=FXCollections.observableArrayList();

		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("name", "张三");
		map1.put("age", 18);
		map1.put("score", 67.9);
		map1.put("is", true);

		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("name", "李四");
		map2.put("age", 30);
		map2.put("score", 98.9);
		map2.put("is", false);


		datas.addAll(map1,map2);

		tableView.setItems(datas);

		TableColumn<Map<String, Object>, String> tc_name=new TableColumn<Map<String, Object>, String>("姓名");
		tc_name.setCellValueFactory(new MapValueFactory("name"));
		/*tc_name.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map<String, Object>,String>, ObservableValue<String>>() {
			@Override
			public ObservableValue<String> call(CellDataFeatures<Map<String, Object>, String> param) {
				SimpleStringProperty sp = new SimpleStringProperty(param.getValue().get("name").toString());
				return sp;
			}
		});*/

		TableColumn<Map<String, Object>, Integer> tc_age=new TableColumn<Map<String, Object>, Integer>("年龄");
		tc_age.setCellValueFactory(new MapValueFactory("age"));
		/*tc_age.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map<String, Object>,Number>, ObservableValue<Number>>(
				) {
					@Override
					public ObservableValue<Number> call(CellDataFeatures<Map<String, Object>, Number> param) {
						System.out.println("年龄1:" + param.getValue());
						System.out.println("年龄2：" + param.getValue().get("age"));
						SimpleIntegerProperty sp = new SimpleIntegerProperty(Integer.parseInt(param.getValue().get("age").toString()));
						return sp;
					}
		});*/

		//注意，这里要使用Number类型，因为只有ObservableValue<Number>这个泛型类
		TableColumn<Map<String, Object>, Number> tc_score=new TableColumn<Map<String, Object>, Number>("分数");
		tc_score.setCellValueFactory(new MapValueFactory("score"));
		/*tc_score.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map<String, Object>,Number>, ObservableValue<Number>>() {
			@Override
			public ObservableValue<Number> call(CellDataFeatures<Map<String, Object>, Number> param) {
				SimpleDoubleProperty sp = new SimpleDoubleProperty(Double.parseDouble(param.getValue().get("score").toString()));
				return sp;
			}
		});*/

		TableColumn<Map<String, Object>, Boolean> tc_is=new TableColumn<Map<String, Object>, Boolean>("是否选中");
		tc_is.setCellValueFactory(new MapValueFactory("is"));
		/*tc_is.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map<String, Object>,Boolean>, ObservableValue<Boolean>>() {

			@Override
			public ObservableValue<Boolean> call(CellDataFeatures<Map<String, Object>, Boolean> param) {
				SimpleBooleanProperty sp =new SimpleBooleanProperty(new Boolean(param.getValue().get("is").toString()));
				return sp;
			}
		});*/

		tableView.getColumns().add(tc_name);
		tableView.getColumns().add(tc_age);
		tableView.getColumns().add(tc_score);
		tableView.getColumns().add(tc_is);



		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				//新增
				//tableView.getItems().add(new Data("A", 19, 55.9, true));

				//删除
				//tableView.getItems().remove(0);

			}
		});

	}
}
